<?php
class MAmazonOrder extends MY_Model
{

    protected $table = 'amazon_order';
    const STATE_UNABLE = 0;
    const STATE_ABLE = 1;

    function __construct()
    {
        parent::__construct();
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function updateOrderTime($month){
        $tableName = 'amazon_order_'.$month;
        $list = $this->getAllNoTime($month);
        foreach ($list as $v){
            $time = strtotime($v->date);
            $v->order_time = $time;
            $arr[] = $v;
        }
        $this->db->update_batch($tableName,$arr,'id');
        return true;
    }

    public function getAllNoTime($date){
        $tableName = 'amazon_order_'.$date;
        $this->db->select('*');
        $this->db->from($tableName);
        $query = $this->db->get();
        return $query->result();
    }

    public function getAllStatic()
    {
        $queryResult = $this->getAll();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    function deleteByStaticId($staticId, $shopId, $date){
        $tableName = 'core_amazon_order_'.$date;
        $sql = "delete from ".$tableName." where import_during_id = ".$staticId ." and shop_id = ".$shopId;
        return $this->db->query($sql);
    }

    public function getAllByStaticId($id){

        $this->db->select('count(o.id) as order_num,s.id as shop_id,s.group_id,s.name as shop_name');
        $this->db->from($this->table.' o');
        $this->db->join('shops s','s.id = o.shop_id and o.import_during_id = '.$id,'INNER');
        $this->db->group_by('o.shop_id');

        $query = $this->db->get();
        return $query->result();
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATE_ABLE => '<span class="label label-success">可用</span>', self::STATE_UNABLE => '<span class="label label-danger">禁用</span>',);
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public function getList($limit, $sort = false)
    {
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($this->table);

        $this->formfilter->doFilter();
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }


    public function getCount()
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);

        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getQuery($limit = null)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($this->table . '.del', UNDELETED);
        if ($limit != null)
            $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();
        $rtn = $this->db->get();
        return $rtn;
    }

}
